<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="demo-ruleForm">
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="ruleForm.projectName"></el-input>
      </el-form-item>
      <el-form-item label="项目介绍" prop="desc">
        <el-input type="textarea" v-model="ruleForm.desc"></el-input>
      </el-form-item>
      <el-form-item label="负责人姓名" prop="leaderName">
        <el-input v-model="ruleForm.leaderName"></el-input>
      </el-form-item>
      <el-form-item label="立项申请书(图片)" prop="url">
        <el-upload
          class="avatar-uploader"
          action=""
          :show-file-list="false"
          :before-upload="beforeUploads"
          :http-request="doPut"
        >
          <img v-if="ruleForm.url" :src="ruleForm.url" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
    export default {
        data() {
            return {
                ruleForm: {
                    projectName: '',
                    desc: '',
                    leaderName: '',
                    url: '',
                },
                rules: {
                    projectName: [
                        { required: true, message: '项目名不能为空', trigger: 'blur' },
                        { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
                    ],
                    desc: [
                        { required: true, message: '项目简介不能为空', trigger: 'blur' }
                    ],
                    leaderName: [
                        { required: true, message: '负责人姓名不能为空', trigger: 'blur' }
                    ],
                    url: [
                        { required: true, message: '请上传立项申请书', trigger: 'blur' }
                    ],
                }
            };
        },
        methods: {
            submitForm(formName) {
                console.log(formName)
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let _this = this
                        this.$post({
                            url : "project/college/apply",
                            data:{
                                name : _this.ruleForm.projectName,
                                profile : _this.ruleForm.desc,
                                leader : _this.ruleForm.leaderName,
                                catForUrl : _this.ruleForm.url,
                                collegeId : _this.$store.getters.getCollegeId
                            }
                        }).then(res =>{
                            alert('提交成功!即将跳转到项目列表');
                            this.$router.push({path:'projects',});
                        })
                    } else {
                        alert('提交失败!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            beforeUploads(file){
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            doPut(option){
                let file = option.file
                let OSS = require('ali-oss');
                this.$post({
                    url : "sts/sign"
                }).then(res =>{
                    let ossClient = new OSS({
                        region: 'oss-cn-qingdao',
                        accessKeyId: res.accessKeyId,
                        accessKeySecret: res.accessKeySecret,
                        stsToken: res.securityToken,
                        bucket: 'lius-1'
                    })
                    ossClient.put("imgs/" + (new Date()).getTime() + ".jpg", file).then(res =>{
                        this.ruleForm.url = res.url
                    })
                }).catch(err => {
                    console.log(err)
                })
            }
        }
    }
</script>

<style scoped>
  .avatar-uploader{
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #409EFF;
    border-radius: 6px;
    cursor: pointer;
    position: absolute;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    border: 1px dashed #409EFF;
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
